//
//  CustomReactionViewController.m
//  autostainer
//
//  Created by lurenjia on 2017/6/4.
//  Copyright © 2017年 walker. All rights reserved.
//

#import "CustomReactionViewController.h"
#import "Global.h"

@interface CustomReactionViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionview;
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (strong, nonatomic) NSArray *dataArray;
@property (strong, nonatomic) NSMutableArray *dataList;
@property (strong, nonatomic) NSMutableArray *modelArray;

@end

@implementation CustomReactionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.dataArray = @[@"无水乙醇",@"水",@"BS液",@"橘黄G",@"伊红",@"HCL",@"EA50",@"DNA染液",@"苏木素",@"95%无水乙醇",@"75%无水乙醇",@"风干"];
    self.dataList = [NSMutableArray array];
    self.modelArray = [NSMutableArray array];
    [self.navigationItem setTitle:self.navTitleStr];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)reloadTableViewData {
    [self.tableview reloadData];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:self.dataList.count-1];
    [self.tableview scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:true];
}
- (IBAction)setting:(UIBarButtonItem *)sender {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    CustomDetailViewController *vc = [sb instantiateViewControllerWithIdentifier:@"CustomDetailViewController"];
    vc.navTitleStr = self.navTitleStr;
    vc.dataArray = self.modelArray;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - TableViewDataSource && TbaleViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (self.dataList.count) {
        
        return self.dataList.count;
    }
    else {
        return 0;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell" forIndexPath:indexPath];
    UILabel *titleLabel = [cell.contentView viewWithTag:1];
    titleLabel.text = self.dataList[indexPath.section];
    int R = (arc4random() % 256) ;
    int G = (arc4random() % 256) ;
    int B = (arc4random() % 256) ;
    [titleLabel setTextColor:[UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1.0]];
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 15)];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 15)];
    label.text = [NSString stringWithFormat:@"第%ld步反应",section+1];
    label.font= [UIFont systemFontOfSize:15];
    label.textColor = [UIColor blackColor];
    [headerView addSubview:label];
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 15;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 5;
}

#pragma mark - CollectionViewDataSource && CollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSLog(@"dataArray.count:%ld",self.dataArray.count);
    NSLog(@"dataArray:%@",self.dataArray);
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ReactionCell" forIndexPath:indexPath];
    UILabel *label = [cell.contentView viewWithTag:5];
    NSAttributedString *attributed = [[NSAttributedString alloc]initWithString:self.dataArray[indexPath.row] attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:12]}];
    [label setAttributedText:attributed];
    int R = (arc4random() % 256) ;
    int G = (arc4random() % 256) ;
    int B = (arc4random() % 256) ;
    [cell setBackgroundColor:[UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1.0]];
    NSLog(@"dataArray%ld:%@",indexPath.row,self.dataArray[indexPath.row]);
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat margin = 10;
    CGFloat height = 30;
    CGFloat width = ([UIScreen mainScreen].bounds.size.width - (margin * 5)) / 4;
    return CGSizeMake(width, height);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    [self.dataList addObject:self.dataArray[indexPath.row]];
    ExperimentModel *model = [[ExperimentModel alloc]init];
    model.name = self.dataArray[indexPath.row];
    [self.modelArray addObject:model];
    [self reloadTableViewData];
}
@end
